package fidus;

import java.sql.*;
import java.text.NumberFormat;
import java.util.Scanner;

public class FidusApp {

    private static ResultSet searchResult;

    public static void main(String[] args) {
        fidusMenu();
    }

    private static void fidusMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("-= FupFidus A/S - Film Database =-");
        System.out.println("Vælg et menupunkt:");
        System.out.println("1: Foretag opslag søgning");
        System.out.println("2: Ret opslag");
        System.out.println("3: Slet opslag");
        System.out.println("4: Nyt opslag");
        System.out.println("5: Import fra XML");
        System.out.println("6: Export til XML");
        System.out.println("7: Afslut \n");
        System.out.print("Valg: ");
        int which = keyboard.nextInt();

        switch (which) {
            case 1:
                searchMovie();
                printMovie();
                break;
            case 2:
                editMovie();
                break;
            case 3:
                deleteMovie();
                break;
            case 4:
                newMovie();
                break;
            case 5:
                System.exit(0);
            case 6:
                System.exit(0);
            case 7:
                System.exit(0);
            default:
                break;
        }
        fidusMenu();
    }

    private static void searchMovie() {
        FidusDBConnect db = new FidusDBConnect();
        Scanner keyboard = new Scanner(System.in);
        db.setAll(false); // nulstiller "søg på alle parameter på databaseobjektet"

        System.out.println("\nHvad vil du søge på?:");
        System.out.println("1: Titel");
        System.out.println("2: Type");
        System.out.println("3: Genre");
        System.out.println("4: År");
        System.out.println("5: Pris");
        System.out.println("6: Alle opslag \n");
        System.out.print("Valg: ");
        int which = keyboard.nextInt();

        switch (which) {
            case 1:
                db.setColumn("title");
                break;
            case 2:
                db.setColumn("type");
                break;
            case 3:
                db.setColumn("genre");
                break;
            case 4:
                db.setColumn("year");
                break;
            case 5:
                db.setColumn("price");
                break;
            case 6:
                db.setAll(true);
                break;
            default:
                System.out.println("Forkert indtastning prøv igen");
                searchMovie();
        }

        if (!db.isAll()) {
            System.out.print("Indtast søgeord: ");
            db.setSearch(keyboard.next());
        } else {
            db.setSearch(null);
        }
        searchResult = db.selectMovie();
    }

    private static void newMovie() {
        Scanner keyboard = new Scanner(System.in);
        FidusDBConnect db = new FidusDBConnect();

        System.out.print("Indtast ønsket titel: ");
        String title = keyboard.nextLine();

        System.out.print("Indtast ønsket type: ");
        String type = keyboard.nextLine();

        System.out.print("Indtast ønsket genre: ");
        String genre = keyboard.nextLine();

        System.out.print("Indtast ønsket årstal: ");
        int year = keyboard.nextInt();

        System.out.print("Indtast ønsket pris: ");
        double price = keyboard.nextDouble();

        Movie m = new Movie(title, type, genre, year, price);

        if (db.insertMovie(m)) {
            System.out.println("\nNyt opslag indsat!\n");
        } else {
            System.out.println("\nOpslag ikke indsat!\n");
        }
    }

    private static void editMovie() {
        Scanner keyboard = new Scanner(System.in);
        FidusDBConnect db = new FidusDBConnect();

        searchMovie();
        printMovie();

        System.out.print("Hvilket opslag ønskes rettet? Indtast ID: ");
        int id = Integer.parseInt(keyboard.nextLine());
                
        System.out.print("Indtast ønsket titel: ");
        String title = keyboard.nextLine();  

        System.out.print("Indtast ønsket type: ");
        String type = keyboard.nextLine();

        System.out.print("Indtast ønsket genre: ");
        String genre = keyboard.nextLine();

        System.out.print("Indtast ønsket årstal: ");
        int year = keyboard.nextInt();

        System.out.print("Indtast ønsket pris: ");
        double price = keyboard.nextDouble();

        Movie m = new Movie(title, type, genre, year, price);

        if (db.updateMovie(m, id)) {
            System.out.println("\nOpslag rettet!\n");
        } else {
            System.out.println("\nOpslag ikke rettet!\n");
        }
    }

    private static void deleteMovie() {
        Scanner keyboard = new Scanner(System.in);
        FidusDBConnect db = new FidusDBConnect();

        searchMovie();
        printMovie();

        System.out.print("Hvilket opslag ønskes slettet? Indtast ID: ");
        int id = keyboard.nextInt();
    
        if (db.deleteMovie(id)) {
            System.out.println("\nOpslag rettet!\n");
        } else {
            System.out.println("\nOpslag ikke rettet!\n");
        }
    }

    private static void printMovie() {
        NumberFormat cf = NumberFormat.getCurrencyInstance();
        System.out.println("\nFundet følgende:");

        int titleLength = 40;
        int genreLength = 20;
        int typeLength = 10;

        try {
            while (searchResult.next()) {
                Movie m = new Movie(searchResult);
                String msg = "ID: " + padAlign(Integer.toString(m.id), 2, ' ', false) + " - ";
                msg += Integer.toString(m.year);
                msg += ": " + padAlign(m.title, titleLength, ' ', false);
                msg += " (" + padAlign(m.genre + ")", genreLength, ' ', false);
                msg += " (" + padAlign(m.type + ")", typeLength, ' ', false);
                msg += " (" + cf.format(m.price) + ")";
                System.out.println(msg);
            }
            System.out.println();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    // String metode til at holde rette længden på en streng så man kan lave "tab" padding
    public static String padAlign(String string, int fieldLength, char padding, boolean alignRight) {
        int length = fieldLength - string.length();

        if (length <= 0) {
            return string.substring(0, string.length());
        }

        StringBuffer buffer = new StringBuffer(fieldLength);

        for (int i = 0; i < length; i++) {
            buffer.append(padding);
        }

        if (alignRight) {
            buffer.append(string);
        } else {
            buffer.insert(0, string);
        }

        return new String(buffer);
    }
}